package com.sjk.flink.correct.rules;

import cn.hutool.core.date.DateUtil;
import com.sjk.flink.correct.IfCorrectRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 统一时间格式 yyyy-MM-dd HH:mm:ss
 */
public class UnifyDateTimeRuler implements IfCorrectRule {

    private static final Logger log = LoggerFactory.getLogger(UnifyDateTimeRuler.class);

    @Override
    public String getName() {
        return "UnifyDateTime";
    }

    @Override
    public Object transformValue(Object val) {
        if(val!=null){
            //日志格式校准
            DateFormat formatTo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = formatDate(String.valueOf(val));
            if(date!=null){
                //时间有效性校准
                String transformDate = formatTo.format(date);
                int year = Integer.parseInt(transformDate.substring(0, 4));
                if(year<2023){
                    //替换年份
                    String dateString = "2023"+transformDate.substring(4,19);
                    try {
                        //如果替换日期大于当前时间使用默认时间
                        Date targetDate = formatTo.parse(dateString);
                        int result = new Date().compareTo(targetDate);
                        if (result < 0) {
                            return "2023-01-01 00:00:00";
                        }
                    }catch (Exception ex){
                    }
                }
                return transformDate;
            }else{
                return "时间格式不正确:"+val;
            }
        }
        //转换不了返回原值
        return val;
    }

    public static void main(String[] args) {
        UnifyDateTimeRuler ruler = new UnifyDateTimeRuler();
        String timestr ="1960612-12-14 01:56:40";
        System.out.println(ruler.transformValue(timestr));
    }

    /**
     * 统一时间格式
     *
     * @param str 字符串
     * @return
     */
    private Date formatDate(String str) {
        if (str == null) {
            return null;
        }
        if (str.contains(",") && (str.contains("AM") || str.contains("PM"))) {
            try {
                DateFormat formatFrom = new SimpleDateFormat("MMM dd,yyyy KK:mm:ss aa", Locale.ENGLISH);
                return formatFrom.parse(str);
            } catch (ParseException e) {
                log.error("时间转换失败：{}", e.getMessage());

            }

        } else if (str.length() == 13) {
            try {
                return new Date(Long.parseLong(str));
            } catch (NumberFormatException e) {
                log.error("时间转换失败：{}", e.getMessage());
            }
        } else {
            try {
                return DateUtil.parse(str);
            }catch (Exception ex){
                //ex.printStackTrace();
                log.error("时间转换失败：{}", ex.getMessage());
                return  null;
            }
        }
        return null;
    }
}
